<?php
require_once 'Database.php';
SecureSoapServer::logSecureSoapServer( 'SecureSoapServer top', 51, 'require_once Database.php');

require_once 'ServerHeaderDOMDocument.php';
SecureSoapServer::logSecureSoapServer( 'SecureSoapServer top', 51, 'require_once ServerHeaderDOMDocument.php');
		


/**
 * A SoapServer that has a header
 * @author Tom Russ
 */
class SecureSoapServer extends SoapServer {
    public static function logSecureSoapServer($source, $level, $description)
    {
        $db = new Database();
        $db->connect();
        $db->logEvent($source, $level, $description);
    }

    private static $dom; //this contains a Document Object Model, which represents all the incoming XML for the SOAP Server.

    /*
     * This overrides the SoapServer handle() method.  Added functionality is header parsing so 
     * other classes can use the extracted DOM.
     */
    public function handle($soap_request = 0)
    {
        try {
            $db = new Database();
            $db->connect();
            $db->logEvent(__METHOD__, 51, 'Enter method');
            if (is_string($soap_request)) 
            {
                $req = $soap_request;
            }
            else
            {
                $req = $this->getSoapRequest();
            }
            self::$dom = new ServerHeaderDOMDocument();
            if ( FALSE === self::$dom->loadXML($req)) {
                $db->logEvent(__METHOD__, 4, 'loadXML failed');
            }
            $db->logEvent(__METHOD__, 52, 'end of try block');
        } catch (Exception $ee) {
                $db->logEvent(__METHOD__, 0, $ee->getMessage());
        }
        $db->logEvent(__METHOD__, 21, $req);
        parent::handle($req);
    }

    /*
    * Extract the SOAP message from the POST data or the raw request body
    */
    private  function getSoapRequest()    {
        global $HTTP_RAW_POST_DATA;
        if($HTTP_RAW_POST_DATA) {
            return $HTTP_RAW_POST_DATA;
        }
        //read the raw request body
        $f = file("php://input");
        return implode(" ", $f);
    }
    /**
     * Return a reference to the DOM for the incoming XML
     * @return XML DOM
     */
    public static function GetDOM()
    {
            return self::$dom;
    }
}
?>